package com.example.pclimb.ui.payment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Toast;

import com.example.pclimb.R;
import com.example.pclimb.databinding.ActivityPaymentBinding;
import com.example.pclimb.model.ClimbRequest;
import com.example.pclimb.ui.requests.RequestDetailActivity;
import com.example.pclimb.viewmodel.ClimbRequestViewModel;

public class PaymentActivity extends AppCompatActivity {

    private ActivityPaymentBinding binding;
    private ClimbRequestViewModel requestViewModel;
    private long requestId;
    private ClimbRequest currentRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPaymentBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 设置工具栏
        setSupportActionBar(binding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        binding.toolbar.setNavigationOnClickListener(v -> onBackPressed());

        // 初始化ViewModel
        requestViewModel = new ViewModelProvider(this).get(ClimbRequestViewModel.class);

        // 获取请求ID
        if (getIntent().hasExtra("request_id")) {
            requestId = getIntent().getLongExtra("request_id", -1L);
            if (requestId == -1L) {
                Toast.makeText(this, R.string.error_loading_data, Toast.LENGTH_SHORT).show();
                finish();
                return;
            }
        } else {
            Toast.makeText(this, R.string.error_loading_data, Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 加载请求详情
        loadRequestDetails();

        // 设置支付按钮点击事件
        binding.btnPay.setOnClickListener(v -> processPayment());
    }

    private void loadRequestDetails() {
        // 显示加载进度条
        binding.progressBar.setVisibility(View.VISIBLE);

        // 获取请求详情
        requestViewModel.getRequestById((int)requestId).observe(this, request -> {
            binding.progressBar.setVisibility(View.GONE);
            if (request != null) {
                currentRequest = request;
                updateUI(request);
            } else {
                Toast.makeText(this, R.string.error_loading_data, Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    private void updateUI(ClimbRequest request) {
        binding.tvLocation.setText(request.getLocation());
        binding.tvDate.setText(request.getDate());
        
        // 格式化价格显示
        String price = "¥" + request.getPrice();
        binding.tvPrice.setText(price);
    }

    private void processPayment() {
        // 显示加载进度条
        binding.progressBar.setVisibility(View.VISIBLE);
        binding.btnPay.setEnabled(false);

        // 获取选择的支付方式
        String paymentMethod;
        if (binding.rbAlipay.isChecked()) {
            paymentMethod = "alipay";
        } else {
            paymentMethod = "wechat";
        }

        // 模拟支付处理
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            // 更新请求状态为已支付
            if (currentRequest != null) {
                currentRequest.setStatus(ClimbRequest.STATUS_COMPLETED);
                requestViewModel.updateRequest(currentRequest);
                
                binding.progressBar.setVisibility(View.GONE);
                binding.btnPay.setEnabled(true);
                
                // 显示支付成功提示
                Toast.makeText(this, R.string.payment_success, Toast.LENGTH_SHORT).show();
                
                // 返回到请求详情页面
                Intent intent = new Intent(this, RequestDetailActivity.class);
                intent.putExtra("request_id", requestId);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
            } else {
                binding.progressBar.setVisibility(View.GONE);
                binding.btnPay.setEnabled(true);
                Toast.makeText(this, R.string.payment_failed, Toast.LENGTH_SHORT).show();
            }
        }, 2000); // 模拟2秒的网络延迟
    }
} 